home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / rbbs_pc / laston11.zip / LASTON.C next >
C/C++ Source or Header  |  1990-10-23  |  11KB  |  412 lines

  1. /*  LASTON.C
  2.  *---------------------------------------------------------------------------
  3.  *
  4.  *  RBBS Callers File Lister
  5.  *
  6.  *  10-22-90
  7.  *
  8.  */
  9. #include <stdio.h>
  10. #include <io.h>
  11. #include <alloc.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15.  
  16. typedef unsigned int uint;
  17. typedef unsigned char uchar;
  18.  
  19. #define TRUE    1
  20. #define FALSE   0
  21.  
  22. #define BLUE    ""
  23. #define RED     ""
  24. #define WHITE   ""
  25. #define YELLOW  ""
  26. #define CYAN    ""
  27.  
  28. struct LStruct
  29.    {
  30.    char       users_name[32];
  31.    char       city_state[25];
  32.    char       date_time[20];
  33.    };
  34.  
  35. char *mix_case(char * );
  36. char *trim(char * );
  37.  
  38. void main(int argc ,char *argv[] )
  39. {
  40.    FILE   *fpcall;
  41.    int    i, len;
  42.    char   one_rec[129];
  43.    uint   recs_in_file;
  44.    uint   max_callers = 5;
  45.    uint   num_callers = 0;
  46.    uint   num_excluded = 0;
  47.    int    name_len = -1;
  48.    int    city_len = -1;
  49.    int    date_len = -1;
  50.    int    same_caller;
  51.    int    display_dupes = FALSE;
  52.    struct LStruct *last_on;
  53.    char   excluded[20][32];
  54.    char   last_caller[32];
  55.    char   *color1_default = RED;
  56.    char   *color2_default = YELLOW;
  57.    char   *color3_default = WHITE;
  58.    char   *color4_default = CYAN;
  59.    char   *color5_default = BLUE;
  60.  
  61.    if (argc < 2 )
  62.       {
  63.       printf("Usage: LASTON <Callers> [/NCx] [/Xn] [/DD] [/FCx] [/TCx] [/UCx] [/CCx] [/DCx]\n" );
  64.       printf("       /NCx - Display x callers (defaults to 5)\n" );
  65.       printf("       /Xn  - Exclude callers with the name n\n" );
  66.       printf("                Use a '_' in place of a ' ' in name, i.e. /XTom_Collins\n" );
  67.       printf("       /DD  - Display duplicate callers (default is to not display 'em)\n" );
  68.       printf("       /FC  - Set frame color string to x\n" );
  69.       printf("                LASTON puts this between a ESC '[' and an 'm'\n" );
  70.       printf("                i.e. /FC1;37 results in the ANSI string ESC [1;37m\n" );
  71.       printf("       /TC  - Set title color string to x\n" );
  72.       printf("       /UC  - Set user's name color string to x\n" );
  73.       printf("       /CC  - Set user's City/State color string to x\n" );
  74.       printf("       /DC  - Set user's date on color string to x\n" );
  75.       printf("\n" );
  76.       printf("Ex: LASTON c:\\rbbs\\callers /NC10 /XTom_Collins /dd /CC1;34\n" );
  77.       return;
  78.       }
  79.  
  80.    printf("LASTON v1.10 - Super Dooper RBBS Last Callers List Maker, by Tom Collins\n\n" );
  81.  
  82.    fpcall = fopen(strupr(argv[1] ) ,"rb" );
  83.    if (fpcall == NULL )
  84.       {
  85.       printf("Can't Find Callers File %s... Aborted.\n" ,argv[1] );
  86.       return;
  87.       }
  88.  
  89.    for (i = 2; i < argc; i++ )
  90.       {
  91.       if (strncmp(strupr(argv[i] ) ,"/NC" ,3 ) == 0 )
  92.          {
  93.          max_callers = atoi(&argv[i][3] );
  94.          if (max_callers < 1 || max_callers > 25 )
  95.             {
  96.             max_callers = 5;
  97.             }
  98.          }
  99.       else if (strncmp(argv[i] ,"/DD" ,3 ) == 0 )
  100.          {
  101.          display_dupes = TRUE;
  102.          }
  103.       else if (strncmp(argv[i] ,"/X" ,2 ) == 0 && num_excluded < 20 )
  104.          {
  105.          int j;
  106.  
  107.          for (j = 2; argv[i][j] != '\0'; j++ )
  108.             {
  109.             if (argv[i][j] == '_' )
  110.                {
  111.                argv[i][j] = ' ';
  112.                }
  113.             }
  114.          strcpy(excluded[num_excluded] ,mix_case(&argv[i][2] ) );
  115.          num_excluded++;
  116.          }
  117.       else if (argv[i][0] == '/' && argv[i][2] == 'C' &&
  118.                strchr("FTUCD" ,argv[i][1] ) != NULL )
  119.          {
  120.          char c;
  121.          int j;
  122.  
  123.          j = strlen(argv[i] );
  124.          c = argv[i][1];
  125.  
  126.          strcpy(&argv[i][2] ,&argv[i][3] );
  127.  
  128.          argv[i][0]   = '\x1B';
  129.          argv[i][1]   = '[';
  130.          argv[i][j-1] = 'm';
  131.          argv[i][j]   = '\0';
  132.  
  133.          switch (c )
  134.             {
  135.             case 'F': color1_default = argv[i]; break;
  136.             case 'T': color2_default = argv[i]; break;
  137.             case 'U': color3_default = argv[i]; break;
  138.             case 'C': color4_default = argv[i]; break;
  139.             case 'D': color5_default = argv[i]; break;
  140.             }
  141.          }
  142.       }
  143.  
  144.    last_on = (struct LStruct *) calloc(sizeof(struct LStruct ) ,max_callers );
  145.  
  146.    last_caller[0] = '\0';
  147.  
  148.    recs_in_file = (uint) (filelength(fileno(fpcall ) ) / 64L);
  149.  
  150.    printf("Reading %s... " ,argv[1] );
  151.  
  152.    for (i = recs_in_file-1; i >= 0 && num_callers < max_callers; i-- )
  153.       {
  154.       same_caller = FALSE;
  155.  
  156.       /*
  157.        * Read a callers record
  158.        */
  159.       fseek(fpcall ,((long)i)*64L ,SEEK_SET );
  160.       if (fread(one_rec ,64 ,1 ,fpcall ) != 1 )
  161.          {
  162.          break;
  163.          }
  164.       if (one_rec[0] != ' ' )
  165.          {
  166.          char *p1, *p2, *p3;
  167.  
  168.          one_rec[64] = '\0';
  169.          strlwr(one_rec );
  170.  
  171.          /*
  172.           * If the current record is a header, read the
  173.           * previous record, too.
  174.           */
  175.          if (((p1 = strstr(one_rec ,"on at" ) ) == NULL ) || (--i < 0 ) )
  176.             {
  177.             continue;
  178.             }
  179.          *p1 = '\0';
  180.  
  181.          strcpy(last_on[num_callers].users_name ,mix_case(trim(one_rec ) ) );
  182.          if (strcmp(last_on[num_callers].users_name ,last_caller ) == 0 )
  183.             {
  184.             same_caller = TRUE;
  185.             }
  186.          strcpy(last_caller ,last_on[num_callers].users_name );
  187.  
  188.          fseek(fpcall ,((long)i)*64L ,SEEK_SET );
  189.          if (fread(&one_rec[64] ,64 ,1 ,fpcall ) != 1 )
  190.             {
  191.             break;
  192.             }
  193.          one_rec[128] = '\0';
  194.          strlwr(p1+1 );
  195.  
  196.          if (same_caller && !display_dupes )
  197.             {
  198.             continue;
  199.             }
  200.          else if (num_excluded != 0 )
  201.             {
  202.             int i, found = FALSE;
  203.  
  204.             for (i = 0; i < num_excluded; i++ )
  205.                {
  206.                if (strcmp(last_on[num_callers].users_name ,excluded[i] ) == 0 )
  207.                   {
  208.                   found = TRUE;
  209.                   break;
  210.                   }
  211.                }
  212.             if (found )
  213.                continue;
  214.             }
  215.  
  216.          if ((p2 = strstr(p1+5 ,"from" ) ) == NULL )
  217.             {
  218.             continue;
  219.             }
  220.  
  221.          *p2 = '\0';
  222.          strcpy(last_on[num_callers].date_time ,strupr(trim(p1+5 ) ) );
  223.  
  224.          if ((p3 = strstr(p2+4 ,"baud" ) ) == NULL )
  225.             {
  226.             continue;
  227.             }
  228.  
  229.          /*
  230.           * Find the last comma before the baud rate
  231.           */
  232.          while (p3 > p2+4 )
  233.             {
  234.             if (*p3 == ',' )
  235.                break;
  236.             p3--;
  237.             }
  238.  
  239.          if (p3 == p2+4 )
  240.             {
  241.             continue;
  242.             }
  243.  
  244.          *p3 = '\0';
  245.          strcpy(last_on[num_callers].city_state ,mix_case(trim(p2+4 ) ) );
  246.  
  247.          num_callers++;
  248.          }
  249.       }
  250.  
  251.    fclose(fpcall );
  252.  
  253.    for (i = 0; i < num_callers; i++)
  254.       {
  255.       len = strlen(last_on[i].users_name );
  256.       if (len > name_len )
  257.          {
  258.          name_len = len;
  259.          }
  260.       len = strlen(last_on[i].city_state );
  261.       if (len > city_len )
  262.          {
  263.          city_len = len;
  264.          }
  265.       len = strlen(last_on[i].date_time );
  266.       if (len > date_len )
  267.          {
  268.          date_len = len;
  269.          }
  270.       }
  271.  
  272.    if (num_callers == 0 )
  273.       {
  274.       printf("No Previous Callers... Done.\n" );
  275.       return;
  276.       }
  277.  
  278.    printf("Writing Output Files... " );
  279.  
  280.    for (i = 0; i < 3; i++)
  281.       {
  282.       int    j, k;
  283.       FILE   *fpout;
  284.       char   single_line[81];
  285.       char   double_line[81];
  286.       char   fmt[80];
  287.       char   temp1[10];
  288.       char   temp2[10];
  289.       char   temp3[10];
  290.       char   *color1 = "";
  291.       char   *color2 = "";
  292.       char   *color3 = "";
  293.       char   *color4 = "";
  294.       char   *color5 = "";
  295.       char   *filename = "LASTONG";
  296.       char   single_line_char = '\xC4';
  297.       char   double_line_char = '\xCD';
  298.  
  299.       switch (i )
  300.          {
  301.          case 0:
  302.             single_line_char = '-';
  303.             double_line_char = '=';
  304.             filename = "LASTON";
  305.             break;
  306.          case 1:
  307.             break;
  308.          case 2:
  309.             color1 = color1_default;
  310.             color2 = color2_default;
  311.             color3 = color3_default;
  312.             color4 = color4_default;
  313.             color5 = color5_default;
  314.             filename = "LASTONC";
  315.             break;
  316.          }
  317.  
  318.       fpout = fopen(filename ,"wt" );
  319.  
  320.       memset(single_line ,single_line_char ,79 );
  321.       memset(double_line ,double_line_char ,79 );
  322.  
  323.       len = (71 - name_len - city_len - date_len ) / 2;
  324.  
  325.       for (j = 0; j < len; j++)
  326.          {
  327.          fmt[j] = single_line[j] = double_line[j] = ' ';
  328.          }
  329.  
  330.       k = name_len + city_len + date_len + len + 8;
  331.  
  332.       single_line[k] = double_line[k] = '\0';
  333.  
  334.       sprintf(&fmt[j] ,"%s%%-%ss    %s%%-%ss    %s%%%ss\n" ,
  335.               color3 ,itoa(name_len ,temp1 ,10 ) ,
  336.               color4 ,itoa(city_len ,temp2 ,10 ) ,
  337.               color5 ,itoa(date_len ,temp3 ,10 ) );
  338.  
  339.       fprintf(fpout ,"\n%s%s\n" ,color1 ,double_line );
  340.       fprintf(fpout ,"                          %sThe Last %i Callers Were...\n" ,color2, num_callers );
  341.       fprintf(fpout ,"%s%s\n" ,color1 ,single_line );
  342.  
  343.       for (j = 0; j < num_callers; j++)
  344.          {
  345.          fprintf(fpout ,fmt ,last_on[j].users_name ,
  346.                              last_on[j].city_state ,
  347.                              last_on[j].date_time );
  348.          }
  349.  
  350.       fprintf(fpout ,"%s%s\n%s{PB\n" ,color1 ,single_line ,i==2? "" : "" );
  351.  
  352.       fclose(fpout );
  353.       }
  354.  
  355.    printf("Done.\n" );
  356. }
  357.  
  358. /*
  359.  *----------------------------------------------------------------------------
  360.  *
  361.  */
  362. char *mix_case(char *s )
  363. {
  364.    int i, upper = TRUE;
  365.  
  366.    for (i = 0; s[i] != '\0'; i++ )
  367.       {
  368.       if (upper )
  369.          {
  370.          s[i] = toupper(s[i] );
  371.          upper = FALSE;
  372.          }
  373.       else
  374.          {
  375.          s[i] = tolower(s[i] );
  376.          }
  377.       if (strchr(" ,.-;:" ,s[i] ) != NULL )
  378.          {
  379.          upper = TRUE;
  380.          }
  381.       }
  382.    return(s );
  383. }
  384.  
  385. /*
  386.  *---------------------------------------------------------------------------
  387.  *
  388.  */
  389. char *trim(char *s )
  390. {
  391.    int i = 0;
  392.  
  393.    while (s[i] == ' ' )
  394.       i++;
  395.  
  396.    if (i != 0 )
  397.       {
  398.       strcpy(s ,&s[i] );
  399.       }
  400.    i = strlen(s );
  401.    if (i != 0 )
  402.       {
  403.       i--;
  404.       while (s[i] == ' ' && i >= 0 )
  405.          {
  406.          s[i] = '\0';
  407.          }
  408.       }
  409.    return(s );
  410. }
  411.  
  412.